<?php

namespace app\common;

class DataService
{
    use InstanceTrait;

    /**
     * 处理发布时间
     * 1、一分钟内显示刚刚
     * 2、一小时内显示x分钟前
     * 3、24小时内显示x小时前
     * 4、大于一天显示x天前
     * 5、大于一个月显示x月前
     * 6、大于一年显示XXXX年xx月xx日
     */
    public function _handleCreateTime($time, $isStrtotime = 0)
    {
        if ($isStrtotime == 0) {
            $time = strtotime($time);
        }

        if (time() - $time < 60) {
            $result = '刚刚';
        } elseif (time() - $time >= 60 && time() - $time < 60 * 60) {
            $result = (int)((time() - $time) / 60) . '分钟前';
        } elseif (time() - $time >= 60 * 60 && time() - $time < 60 * 60 * 24) {
            $result = (int)((time() - $time) / 60 / 60) . '小时前';
        } elseif (time() - $time >= 60 * 60 * 24 && time() - $time < 60 * 60 * 24 * 30) {
            $result = (int)((time() - $time) / 60 / 60 / 24) . '天前';
        } elseif (time() - $time >= 60 * 60 * 24 * 30 && time() - $time < 60 * 60 * 24 * 30 * 12) {
            $result = (int)((time() - $time) / 60 / 60 / 24 / 30) . '月前';
        } else {
            $result = date('Y-m-d', $time);
        }

        return $result;
    }

    /**
     * 处理发布时间
     * 1、1分钟内显示刚刚
     * 2、一小时内显示XX分钟前
     * 3、24小时内显示XX小时前
     * 4、大于24小时显示MM-DD
     * 5、大于一年显示YY-MM-DD
     */
    public function _handleCreateTimeTwo($time, $isStrtotime = 0)
    {
        if ($isStrtotime == 0) {
            $time = strtotime($time);
        }

        if (time() - $time < 60) {
            $result = '刚刚';
        } elseif (time() - $time >= 60 && time() - $time < 60 * 60) {
            $result = (int)((time() - $time) / 60) . '分钟前';
        } elseif (time() - $time >= 60 * 60 && time() - $time < 60 * 60 * 24) {
            $result = (int)((time() - $time) / 60 / 60) . '小时前';
        } elseif (time() - $time < 60 * 60 * 24 * 30 * 12) {
            $result = date('m-d', $time);
        } else {
            $result = date('Y-m-d', $time);
        }

        return $result;
    }

    /*
     * 获取本周一开始时间时间戳
     */
    public function dateDayOneTime()
    {
        $date = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)) . ' 00:00:00';
        return strtotime($date);
    }

    /*
     * 获取本周日结束时间
     */
    public function dateDayWeekTime()
    {
        $date = date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600)) . ' 23:59:59';
        return strtotime($date);
    }

    /*
     * 获取上几周的周一时间戳
     * @param @num 上几周的周一
     */
    public function lastDateDayOneTime($num = 1)
    {
        $date = date('Y-m-d', strtotime("-$num monday", time())) . ' 00:00:00';
        return strtotime($date);
    }

    /*
     * 获取上几周周日的时间戳
     * @param 上几周的周末
     */
    public function lastDateDayWeekTime($num = 1)
    {
        $date = date('Y-m-d', strtotime("-$num sunday", time())) . ' 23:59:59';
        return strtotime($date);
    }


    /*
     * 获取当前年的第几周
     * @param 年周 $num
     */
    public function getYearDay($number = [])
    {
        $time = [];
        if (empty($number)) {
            $time['start'] = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)) . ' 00:00:00';
            $time['end'] = date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600)) . ' 23:59:59';
        } else {
            $start = date('Y-m-d', strtotime("-$number monday", time())) . ' 00:00:00';
            $d = date('W', strtotime("-$number monday", time()));
            if ($d == date('W', strtotime($start))) {
                $y = $number + 1;
                $time['start'] = date('Y-m-d', strtotime("-$y monday", time())) . ' 00:00:00';
            } else {
                $time['start'] = date('Y-m-d', strtotime("-$number monday", time())) . ' 00:00:00';
            }
            $time['end'] = date('Y-m-d', strtotime("-$number sunday", time())) . ' 23:59:59';
        }
        return $time;
    }
}
